from google.appengine.api import urlfetch
from google.appengine.ext import webapp
import urllib, logging
from django.utils import simplejson
from iform_util import *
#from process_data import update_compaction_project_table

class IformHandler(webapp.RequestHandler):
    def get(self):
        action = self.request.get('action')
        if action:
            self.post()
            return
        html = '''
<form enctype="multipart/form-data" action="/iform" method="post">
    <label>url:</label><input type="text" name="url" value="https://www.iformbuilder.com/exzact/dataJSON.php"/><br/>
    <label>username:</label><input type="text" name="username" value="ymeng@dlz.com"/><br/>
    <label>password:</label><input type="text" name="password" value="dlzlab"/><br/>
    <label>page id:</label><input type="text" name="page_id" value="5827"/><br/>
    <label>table name:</label><input type="text" name="table_name" value="_data10835_compaction_test"/><br/>
    
    <label>SINCE ID:</label><input type="text" name="since_id" /><br/>
    <input type="submit" value="Download"/>
</form> 
<form enctype="multipart/form-data" action="/iform" method="post">
    <label>url:</label><input type="text" name="url" value="https://www.iformbuilder.com/exzact/dataJSON.php"/><br/>
    <label>username:</label><input type="text" name="username" value="ymeng@dlz.com"/><br/>
    <label>password:</label><input type="text" name="password" value="dlzlab"/><br/>
    <label>page id:</label><input type="text" name="page_id" value="5827"/><br/>
    <label>record_id:</label><input type="text" name="record_id" value="110"/><br/>
    <label>table name:</label><input type="text" name="table_name" value=" _data10835_compaction"/><br/>
    <input type="submit" value="Download Record"/>
</form>
<form enctype="multipart/form-data" action="/iform" method="post">
    <label>Action:</label><input type="text" name="action" value="update_client"/><br/>
    <input type="submit" value="Update"/>
</form> 
'''
        html = '''
        <a href="/iform?action=sync_compaction_form">Sync compaction_form</a><br/>
        <a href="/iform?action=sync_compaction_test">Sync compaction_test</a>
        '''
        self.response.out.write(html)
    def post(self):
        '''process the iform builder update xml'''
        action = self.request.get('action')
        if action=='update_client':
            update_compaction_project_table()
            self.response.out.write('table updated')
            return
        elif action=='sync_compaction_form':
            sync_compaction_form(0)
            self.response.out.write('compaction_form synced')
        elif action=='sync_compaction_test':
            sync_compaction_test(0)
        else:
            url = self.request.get('url')
            username = self.request.get('username')
            password = self.request.get('password')
            page_id = self.request.get('page_id')
            record_id= self.request.get('record_id')
            table_name = self.request.get('table_name')
            
            since_id = self.request.get('since_id')
            #self.response.out.write(','.join((username, password,page_id, table_name)))
            itable = IformTable(username, password,page_id, table_name)
            if record_id:
                data = itable.get(record_id)
            else:
                data = itable.query(since_id)
           
            self.response.headers["Content-Type"] = "application/json"    
            self.response.out.write(simplejson.dumps(data))
    

        

        